package third.lib.zfb.realnameauth;


import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.DescribeFaceVerifyResponse;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyRequest;
import com.aliyuncs.cloudauth.model.v20190307.InitFaceVerifyResponse;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import third.lib.common.exception.ThirdException;
import third.lib.common.logger.ThirdLogger;
import third.lib.common.status.ThirdStatus;
import third.lib.utils.DateTimeUtils;
import third.lib.utils.StringRandUtils;
import third.lib.zfb.realnameauth.config.RealNameAuthConfig;

@Service
public class ZfbRealNameAuth {
    private final Logger logger = LoggerFactory.getLogger(ZfbRealNameAuth.class);
    @Autowired
    RealNameAuthConfig config;
    @Autowired
    ThirdLogger thirdLogger;

    public ZfbRealNameAuth() {
    }

    public String request(String name, String identity, String metaInfo) throws ThirdException {
        thirdLogger.info("支付宝实名认证配置文件信息: " + JSON.toJSONString(this.config));
        if (this.config.getKey() != null && !this.config.getKey().isEmpty()) {
            if (this.config.getPrefixOrderNo() != null && !this.config.getPrefixOrderNo().isEmpty()) {
                if (this.config.getKey() != null && !this.config.getKey().isEmpty()) {
                    if (this.config.getSceneId() == null) {
                        throw new ThirdException(ThirdStatus.ZFB);
                    } else {
                        InitFaceVerifyRequest request = new InitFaceVerifyRequest();
                        request.setMetaInfo(metaInfo);
                        request.setCertName(name);
                        request.setCertNo(identity);
                        request.setCertType("IDENTITY_CARD");
                        request.setSceneId(this.config.getSceneId());
                        request.setOuterOrderNo(this.config.getPrefixOrderNo() + DateTimeUtils.getMillis() + StringRandUtils.randNumberAndChar(11));
                        request.setProductCode("ID_PRO");
                        request.setModel("LIVENESS");
                        InitFaceVerifyResponse response = null;
                        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", this.config.getKey(), this.config.getSecret());
                        DefaultAcsClient client = new DefaultAcsClient(profile);

                        try {
                            response = (InitFaceVerifyResponse)client.getAcsResponse(request);
                        } catch (Exception var9) {
                            var9.printStackTrace();
                            throw new ThirdException(ThirdStatus.NET);
                        }

                        if (!response.getCode().equals("200")) {
                            this.logger.error("realname request: " + JSON.toJSONString(response));
                            throw new ThirdException(ThirdStatus.ZFB);
                        } else {
                            return response.getResultObject().getCertifyId();
                        }
                    }
                } else {
                    throw new ThirdException(ThirdStatus.ZFB);
                }
            } else {
                throw new ThirdException(ThirdStatus.ZFB);
            }
        } else {
            throw new ThirdException(ThirdStatus.ZFB);
        }
    }

    public boolean query(String certifyId) throws ThirdException {
        DescribeFaceVerifyRequest request = new DescribeFaceVerifyRequest();
        request.setCertifyId(certifyId);
        request.setSceneId(this.config.getSceneId());
        DescribeFaceVerifyResponse response = null;
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", this.config.getKey(), this.config.getSecret());
        DefaultAcsClient client = new DefaultAcsClient(profile);

        try {
            response = (DescribeFaceVerifyResponse)client.getAcsResponse(request);
        } catch (Exception var7) {
            var7.printStackTrace();
            throw new ThirdException(ThirdStatus.NET);
        }

        if (!response.getCode().equals("200")) {
            this.logger.error("realname query: " + JSON.toJSONString(response));
            return false;
        } else if (!response.getResultObject().getSubCode().equals("200")) {
            this.logger.error("realname query: " + JSON.toJSONString(response));
            return false;
        } else {
            return true;
        }
    }
}
